<!-- 单个玩家调控 -->
<template>
  <el-dialog v-if="props.show" :modelValue="props.show" title="玩家调控" width="60%" top="12vh" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="handleClose">
    <template #header>
      <p class="box-title marginB5">【 <span class="mustPass">{{ props.checkedItem?.account3d_id }}</span> 】{{
        props.checkedItem?.account3d }}</p>
    </template>
    <div class="dialogMax">
      <div class="padding10">
        <form-item :title="t('768')" :pass="true" value="game_id">
          <el-select class="imWidth100" v-model="data.userControl.game_ids" :placeholder="t('340')" filterable multiple
            @change="chooseGameType">
            <el-option :label="t('21')" :value="-1" />
            <el-option v-for="(item, index) in indexStore.gameArray" :key="index"
              :label="'【' + item.value + '】' + GameName(indexStore.gameArray,item.value)" :value="item.value" v-show="item.type == 1 ? true : false" />
          </el-select>
        </form-item>
        <form-item title="RTP" :pass="true" value="rtp">
          <el-select class="width150" v-model="data.userControl.rtp" :placeholder="t('340')" filterable>
            <el-option v-for="(item, index) in data.rtpList" :key="index" :label="item+'%'" :value="item" v-show="item==200?data.agentTypeShow:true"/>
          </el-select>
          <el-button class="marginL20" type="success" :disabled="data.userControl.game_ids.length?false:true" @click="addControl">{{t('777')}}</el-button>
        </form-item>
      </div>
      <div class="marginT10">
        <div class="flex marginB10">
          <el-button class="marginR20" type="danger" :disabled="data.chooseControlList.length?false:true" @click="data.isControlShow = true">{{t('775')}}</el-button>
          {{t('866')}}： <el-input-number v-model="data.auto_cancel_rtp" :min="0" :max="99999999999999999999" :precision="0"
          class="width150" :placeholder="t('19')" clearable :controls="false" />%
        </div>
        <el-table :data="data.userGameControlList" border style="height: 430px;" @select-all="chooseControlRtp" @select="chooseControlRtp">
          <el-table-column type="selection" width="45" />
          <el-table-column prop="game_id" :label="t('36')" #default="scope">
            <div class="gameIcon flexCenter">
              <div class="flex">
                <el-tag size="small">{{ GamePlatform(indexStore.gameArray, scope.row.game_id) }}</el-tag>
                <img class="game-icon" :src="selectToLabel(GAMEICON, scope.row.game_id)" alt="">
              </div>
              {{ GameName(indexStore.gameArray, scope.row.game_id) }}
            </div>
          </el-table-column>
          <el-table-column prop="rtp" label="RTP" #default="scope">
            {{ BigHundred(scope.row.rtp) }}%
          </el-table-column>
          <el-table-column prop="create_at" :label="t('767')" #default="scope">
            {{ setTimestampTime(scope.row.create_at) }}
          </el-table-column>
          <el-table-column prop="auto_cancel_rtp" :label="t('866')" #default="scope">
            {{ scope.row.auto_cancel_rtp?BigHundred(scope.row.auto_cancel_rtp)+'%':'-' }}
          </el-table-column>
          <el-table-column fixed="right" :label="t('97')" width="180">
            <template #default="scope">
              <el-button :type="scope.row.user_rtp_game_info ? 'success' : 'danger'" 
                size="small" @click="delControl(scope.row)">
                {{ t('769') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </el-dialog>

  <!-- 批量调控 -->
  <el-dialog :modelValue="data.isControlShow" title="批量调控" width="45%" top="25vh" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="data.isControlShow = false">
    <template #header>
      <p class="box-title marginB5">{{t('765')}}【 <span class="mustPass">{{ props.checkedItem?.account3d_id }}</span> 】{{
        props.checkedItem?.account3d }} </p>
    </template>
    <div class="flexWrap">
      <span class="margin5" v-for="(item,index) in data.chooseControlList" :key="index">
        <div class="gameIcon flexCenter">
          【<span class="colorRed">{{ item.game_id }}</span>】
          <img class="game-icon marginR2" :src="selectToLabel(GAMEICON, item.game_id)" alt="">
          {{ GameName(indexStore.gameArray, item.game_id) }}
        </div>
      </span>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="data.isControlShow = false">{{t('240')}}</el-button>
        <el-button type="primary" @click="delAllControl">{{t('241')}}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import FormItem from '@/components/FormItem/FormItem.vue'
import { getControlUser, changeAllControlUser, changeControlUser } from '@/api/user'
import { getOperatorTiger } from '@/api/settion'
import { selectToLabel, throttle, GamePlatform,GameName } from '@/utils/index'
import { BigHundred, BigTimeHundred } from '@/utils/math'
import { GAMEICON } from '@/config/eunm'
import { setTimestampTime } from '@/utils/time'

import { useStore } from '@/stores/index'
const indexStore = useStore()
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

const props = defineProps({
  show: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  checkedItem: {
    type: Object,
    default: () => {
      return {} as any
    }
  },
})

const data = reactive({
  rtpList: [] as any[],
  userControl: {
    agent_id: 0,
    game_ids: [],
    user_ids: [],
    rtp: 70,
    switch: 1,
  },
  auto_cancel_rtp: '',
  userGameControlList:[] as any[],
  chooseControlList:[] as any[],
  isControlShow: false,
  agentTypeShow:JSON.parse(localStorage.getItem('userInfo') as string).agent_type?true:false
})

watch(
  () => [props.checkedItem,props.show],
  async (value) => {
    if (value[0] && value[1]) {
      getGameRtp()
      getUserRtp(value[0])
    }
  }
)

// 游戏RTP
const getGameRtp = async () => {
  let res = await getOperatorTiger({
    agent_id: 0, game_id: 39
  })
  data.rtpList = res.data.rtpList
}

// 获取玩家RTP调控
const getUserRtp = async (row: any) => {
  data.userControl.agent_id = row.agent_id
  data.userControl.user_ids = [row.account3d_id] as any
  let res = await getControlUser({page:1,size:300,search_type: 1,search_value: row.account3d_id,agent_id:row.agent_id})
  data.userGameControlList = res.data.list
}
const addControl = throttle(async () => {
  let query = JSON.parse(JSON.stringify(data.userControl));
  query.switch = 2
  query.rtp = BigTimeHundred(query.rtp)
  // 选择全部
  const allGame = query.game_ids.find((item: number) => item == -1)
  if (allGame) {
    query.game_ids = []
    indexStore.gameArray.forEach((item: any) => {
      if(item.type ==1){
        query.game_ids.push(item.value)
      }
    })
  }
  await changeControlUser(query)
  ElMessage.success(t('764'))
  onHideDetails()
}, 3000)

// 取消RTP调控
const chooseControlRtp = (val: any) => {
  data.chooseControlList = val
}
const delControl = async (row:any) => {
  let query = JSON.parse(JSON.stringify(data.userControl)),message='';
  query.switch = 1
  query.rtp = 0
  query.game_ids = [row.game_id]
  query.auto_cancel_rtp = data.auto_cancel_rtp

  message = `【${row.game_id}】${GameName(indexStore.gameArray, row.game_id)}`
  ElMessageBox.confirm( t('241')+ t('240')+'( '+message+' )'+t('763'), t('650'), {
    confirmButtonText: t('241'),
    cancelButtonText: t('240')
  }).then(async () => {
    await changeControlUser(query)
    onHideDetails()
    ElMessage.success(t('772'))
  }).catch(() => {})
}
// 批量取消RTP调控
const delAllControl = async () => {
  let query = {} as any, message = '';
  query.user_ids = []
  query.game_ids = [0]
  data.chooseControlList.forEach((item: any) => {
    query.user_ids.push(item.agent_id + '_' + item.account3d_id +'_' + item.game_id)
  })
  query.switch = 1
  query.rtp = 0
  query.auto_cancel_rtp = data.auto_cancel_rtp

  if(!query.user_ids.length){
    return ElMessage.error(t('774'))
  }
  await changeAllControlUser(query)
  ElMessage.success(t('772'))
  onHideDetails()
}

// 选择全部游戏
const chooseGameType = async (row: any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.userControl.game_ids = [-1] as any
  } else {
    data.userControl.game_ids = row
  }
}

// 重置
const emit = defineEmits(['onHideDetails'])
const onHideDetails = async () => {
  data.userControl = {
    agent_id: 0,
    game_ids: [],
    user_ids: [],
    rtp: 70,
    switch: 1,
  }
  data.isControlShow = false
  data.userGameControlList = [] as any[]
  data.chooseControlList = [] as any[]
  getUserRtp(props.checkedItem)
}
const handleClose = async () => {
  onHideDetails()
  emit('onHideDetails', true)
}
</script>

<style scoped lang="scss"></style>
